<?php

require("barDB.php");

$timezone = new DateTime();
//$timezone = $timezone->getTimezone()->getName();
//print "Timezone: $timezone <br/>\n";

$offset = timezone_offset_get( new DateTimeZone( 'UTC' ), new DateTime() );

//$utcZone = new DateTimeZone("UTC");
//$timezone = $utcZone->getName();
//print "UTC: $timezone <br/>\n";


$here = new DateTime();

$offset = $timezone->getTimezone()->getOffset( new DateTime() );
print "Timezone: $offset <br/>\n";


$db = new BeerDB();

print date(DATE_RFC2822);
print "<br/>\n";
print "<br/>\n";

print "Trying to drop all tables....<br/>\n";
$db->exec('DROP TABLE beer');
$db->exec('DROP TABLE tap');
$db->exec('DROP TABLE kegtype');
$db->exec('DROP TABLE temperature');
$db->exec('DROP TABLE remaining');

print "Trying to create all tables....<br/>\n";
$result = true;

$result |= $db->exec( 'CREATE TABLE beer (id INTEGER PRIMARY KEY AUTOINCREMENT, beername TEXT, description TEXT, style TEXT, abv REAL, ibu INT, srm REAL)' );
$result |= $db->exec( 'CREATE TABLE tap  (id INTEGER PRIMARY KEY, beer_id INT, kegtype_id INT, tapepochdate INT)' );
$result |= $db->exec( 'CREATE TABLE remaining (tap_id INT, percentremaining)' );
$result |= $db->exec( 'CREATE TABLE kegtype (id INTEGER PRIMARY KEY AUTOINCREMENT, capacityinoz INT, kegname TEXT)' );
$result |= $db->exec( 'CREATE TABLE temperature (temperature REAL, temperatureepochdate INT)' );

if( true )
{
    $result |= date_default_timezone_set( 'America/Los_Angeles' );
    
    {
        print "Injecting dummy kegtypes <Br/>\n";
        
        $result |= $db->AddKeg( "Croney Keg", (int)floor(5.0 * 128) );
        $result |= $db->AddKeg( "Sixth Barrel", (int)floor(5.17 * 128) );
        
        $kegtypeTable = $db->DumpKegtypeTable();
        while( $row = $kegtypeTable->fetchArray( SQLITE3_ASSOC ) )
        {
            var_dump( $row );
        }
    }
    print "Result: $result <br>\n";
    
    
    {
        print "Injecting dummy temperture data <br/>\n";
        
        // Start date
	    $date = strtotime( "-10 days" );
	    
        // End date
	    $end_date = time();
 
        $temperture = 38.0;
 
	    while( $date <= $end_date )  
        { 
            $date = strtotime( "+4 hours", $date );
		    $strdate = date( "Y-m-d", $date );
            
            print "Date: ($strdate) $date <br/>\n";
            
            $result |= $db->AddTemperature( $temperture, $date );
            
            $temperture += ( (float)mt_rand() / (float)mt_getrandmax() ) - 0.5;
	    }
        
        $tempertureTable = $db->DumpTemperatureTable();
        print "Temperature Table: " . gettype( $tempertureTable ) . " <br/>\n";

        while( $row = $tempertureTable->fetchArray( SQLITE3_ASSOC ) )
        {
            var_dump( $row );
        }
    }
    print "Result: $result <br>\n";
    
    {
        print "Injecting dummy beer data<br/>\n";

        $result |= $db->AddBeer( "Moose Drool", "The world's premier brown ale", "American Brown Ale", 5.1, 26, 34.1 );
        $result |= $db->AddBeer( "Boddingtons Pub Ale", "Renowned for its golden color, distinctive creamy head, smooth body and easy drinking character, Boddingtons is a medium-bodied pale ale. It has a creamy, malty and slightly sweet flavor and features a clean, pleasant aftertaste", "English Pub Ale", 4.6, 28, 2.4 );
        $result |= $db->AddBeer( "Sam Adams", "Sam Adams Brewing Company - Full bodied American Lagar", "Boston Lager", 4.75, 30, 2.2 );

        print "<br/>\n";
        print "<br/>\n";
        print "Dumping beer table <br/>\n";
        
        $beerTable = $db->DumpBeerTable();
        print "Beer Table: " . gettype( $beerTable ) . " <br/>\n";

        while( $row = $beerTable->fetchArray( SQLITE3_ASSOC ) )
        {
            var_dump( $row );
        }
    }
    print "Result: $result <br>\n";

    {
        print "Injecting dummy tap data<br/>\n";

        $date = strtotime("15 November 2013");
        $result |= $db->AddTap( 0, 1, 1, $date );
        
        $date = strtotime("30 January 2013");
        $result |= $db->AddTap( 1, 2, 2, $date );
        
        $tapTable = $db->DumpTapTable();
        print "Tap Table: " . gettype( $tapTable ) . " <br/>\n";

        while( $row = $tapTable->fetchArray( SQLITE3_ASSOC ) )
        {
            var_dump( $row );
        }
    }
    
   
    print "Result: $result <br>\n";
    
    {
        print "Injecting dummy remaining data<br>\n";
        
        $db->SetTapPercentRemaining( 0, 1 );
        $db->SetTapPercentRemaining( 0, 2 );
        $db->SetTapPercentRemaining( 0, 20 );
        
        $db->SetTapPercentRemaining( 1, 70 );
        
        $remainingTable = $db->DumpRemainingTable();
        while( $row = $remainingTable->fetchArray( SQLITE3_ASSOC ) )
        {
            var_dump( $row );
        }
    }
    
}

$db->close();

print "DONE!<br/>\n";

?>